/******************************************************************************
 *
 * MotionViewer
 * Copyright (C) 2007 Sangyoon Lee, Electronic Visualization Laboratory, 
 * University of Illinois at Chicago
 *
 * This software is free software; you can redistribute it and/or modify it 
 * under the terms of the GNU Lesser General Public License as published by
 * the Free Software Foundation; either Version 2.1 of the License, or 
 * (at your option) any later version.
 *
 * This software is distributed in the hope that it will be useful, but
 * WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY
 * or FITNESS FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public 
 * License for more details.
 * 
 * You should have received a copy of the GNU Lesser Public License along
 * with this software; if not, write to the Free Software Foundation, Inc., 
 * 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
 *
 * Questions or comments about MotionViewer should be directed to 
 * cavern@evl.uic.edu
 *
 *****************************************************************************/

/*
* -----------------------------------------------------------------------------
* Filename: LLXMLParser.h
* -----------------------------------------------------------------------------
* Notes:    
* -----------------------------------------------------------------------------
*/

#ifndef __LLXMLPARSER_H_
#define __LLXMLPARSER_H_


#include <xercesc/util/PlatformUtils.hpp>
#include <xercesc/parsers/XercesDOMParser.hpp>
#include <xercesc/dom/DOM.hpp>
#include <xercesc/sax/HandlerBase.hpp>
#include <xercesc/util/XMLString.hpp>
#include <xercesc/util/PlatformUtils.hpp>
#include <xercesc/framework/LocalFileFormatTarget.hpp>
#include <xercesc/framework/Wrapper4InputSource.hpp>
#include <xercesc/framework/MemBufInputSource.hpp>

#define DATA_ELEMENTONLY	0
#define DATA_INT			1 
#define DATA_CHAR			2 
#define DATA_FLOAT			3 

XERCES_CPP_NAMESPACE_USE

// choose your indent string:
static const unsigned indent_size = 3;
static const XMLCh indent_string[indent_size] = {chSpace, chSpace, chSpace};

// choose your newline string:
static const unsigned newline_size = 1;
static const XMLCh newline_string[newline_size] = {chCR};

class LLXMLParser
{

private:

	DOMImplementation*	m_pImpl;		// Main DOM Implementation 
	XERCES_CPP_NAMESPACE::DOMElement*			m_pRootElem;	// Root Element of the Document is stored
	XERCES_CPP_NAMESPACE::DOMDocument* m_pDoc;

public:

	bool		initialize();
	void		terminate();
	void		destroyDoc();

	// document
	void		writeToFile(const char *filename);
	char*		getXMLString();
	void		makePrettyPrint(DOMNode *node);

	// create document
	void		createDocumentFromFile(const char *filename);
	bool		createDocumentFromString(const char *str);
	bool		createDocument(const char* data);
	
	// add element with tag value
	DOMElement*	addElement(char *node_tag, char* node_name , void *data, char DATA_TYPE, int len=1);
	DOMElement*	addElement(DOMElement* parent, char* node_name , void *data, char DATA_TYPE, int len=1);
	
	// attribute setter/adder
	void		addAttribute(DOMElement* element, char* att_name, char* value);
	void		addFloatAttribute(DOMElement* element, char* att_name, float value);
	void		addIntAttribute(DOMElement* element, char* att_name, int value);
	void		addCharAttribute(DOMElement* element, char* att_name, char* value);

	// element getter
	DOMElement*		getRootElement() { return m_pRootElem;}
	DOMNodeList*	getElements(char* node_name);
	DOMNodeList*	getChildren(DOMElement* element, char* tag_name=NULL);
	DOMNode*		getFirstChild(DOMElement* element);
	DOMNode*		getNextSibling(DOMElement* element);

	// tag value getter
	float		getFloatFromTag(char *tag);
	int			getIntFromTag(char *tag);
	char*		getCharFromTag(char *tag);
	int*		getIntegerArray(char *tag);
	float*		getFloatArray(char *tag);
	
	// attribute getter
	float		getFloatAttribute(DOMElement* element, char* att_name);
	int			getIntAttribute(DOMElement* element, char* att_name);
	char*		getCharAttribute(DOMElement* element, char* att_name);

	LLXMLParser();
	virtual ~LLXMLParser();

};


#endif